/**
 *  列表页
 *
 * 作者 Lujiachen
 * 日期 2024-08-12 09:56:23
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-incentiveRewardConfig'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#incentiveRewardConfig-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        tableIns;

    form.verify(validate);
    form.render();
    initFormValue();
    function initFormValue() {
        if (incentiveRewardConfig) {
            form.val("config-form", {
                "rewardConfigId": incentiveRewardConfig.rewardConfigId,
                "tenantId": incentiveRewardConfig.tenantId,
                "rewardAmountTax": incentiveRewardConfig.rewardAmountTax,
                "scoreAmountRatio": incentiveRewardConfig.scoreAmountRatio,
            });

        }
    }

    function reloadPage() {
            var tenantId=$tenantSelect.val();
            febs.get(ctx + 'incentiveRewardConfig/search/' + tenantId, null, function (data) {
                var newInincentiveRewardConfig = "";
               if(data.code==200&&data.data.incentiveRewardConfig){
                   newInincentiveRewardConfig = data.data.incentiveRewardConfig;
               }
                form.val("config-form", {
                    "rewardConfigId": newInincentiveRewardConfig.rewardConfigId,
                    "tenantId": newInincentiveRewardConfig.tenantId,
                    "rewardAmountTax": newInincentiveRewardConfig.rewardAmountTax,
                    "scoreAmountRatio": newInincentiveRewardConfig.scoreAmountRatio,
                });
            })
    }

    $query.on('click', function () {
        reloadPage();
    });

    form.on('submit(config-form-submit)', function (data) {
        if(data.field.tenantId==null || data.field.tenantId ==''){
            data.field.tenantId = $tenantSelect.val();
        }
        febs.modal.confirm('保存提示', '是否保存安全奖励设定？', function () {
            if (data.field.rewardConfigId && data.field.rewardConfigId.length > 0) {
                febs.postArray(ctx + 'incentiveRewardConfig/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                });
            } else {
                febs.postArray(ctx + 'incentiveRewardConfig', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                    reloadPage();
                });
            }
        });

        return false;
    });

});